local ads__qianggong = fk.CreateSkill {
  name = "ads__qianggong",
}

ads__qianggong:addEffect(fk.EventPhaseStart, {
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(ads__qianggong.name) and player.phase == Player.Discard and not data.phase_end and
      table.find(player.room.alive_players, function(p)
        return p ~= player and not p:isKongcheng()
      end)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room.alive_players, function(p)
      return p ~= player and not p:isKongcheng()
    end)

    local discard_num = player:getHandcardNum() - player:getMaxCards()
    if discard_num <= 0 then return false end

    local result = room:askToChoosePlayers(player, {
      targets = targets,
      min_num = 1,
      max_num = discard_num,
      prompt = "#qianggong-ask",
      skill_name = ads__qianggong.name,
    })
    if #result > 0 then
      room:sortByAction(result)
      event:setCostData(self, {tos = result})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    data.phase_end = true
    for _, p in ipairs(event:getCostData(self).tos) do
      if player.dead then break end
      if not p.dead then
        if not p:isKongcheng() then
          local handcards = p:getCardIds(Player.Hand)
          local y = #handcards - 1
          if y > 0 then
            local to_obtain = room:askToChooseCards(player, {
              target = p,
              flag = "h",
              min = y,
              max = y,
              prompt = string.format("请选择获得%s的%d张手牌", p.general, y),
              skill_name = ads__qianggong.name,
            })
            if #to_obtain > 0 then
              for _, card in ipairs(to_obtain) do
                room:obtainCard(player, card, false, fk.ReasonPrey, player, ads__qianggong.name)
              end
            end
          end
        end
      end
    end
  end,
})







Fk:loadTranslationTable{
  ["$ads__qianggong1"] = "哼，没想到吧！",
  ["$ads__qianggong2"] = "拿来吧！",
  ["ads__qianggong"] = "强攻",
  [":ads__qianggong"] = "弃牌阶段，你可以改为令至多X名其他角色只保留各1张手牌，然后你获得之（X为你本应弃牌的数量）。",
  ["#qianggong-ask"] = "是否发动“强攻”，改为令至多X名其他角色只保留各1张手牌，然后你获得之（X为你本应弃牌的数量）？",
}

ads__qianggong:addAI({
  think = function(self, ai)
    local player = ai.player
    local discard_num = player:getHandcardNum() - player:getMaxCards()
    if discard_num <= 0 then return "" end
    
    local targets = table.filter(player.room.alive_players, function(p)
      return p ~= player and not p:isKongcheng()
    end)
    
    if #targets == 0 then return "" end
    local selected_targets = {}
    for i = 1, math.min(discard_num, #targets) do
      table.insert(selected_targets, targets[i])
    end
    
    if #selected_targets == 0 then return "" end
    
    local total_benefit = 0
    for _, target in ipairs(selected_targets) do
      local handcard_num = #target:getCardIds(Player.Hand)
      total_benefit = total_benefit + (handcard_num - 1)
    end
    
    if total_benefit <= 0 then return "" end
    
    return { targets = selected_targets }, total_benefit
  end,
})

return ads__qianggong
